<template>
  <div class="app-container">
    <div class="title-btns">
      <span class="role-span"><span></span>测试树形数据列表</span>
      <div style="float: right">
        <!-- 新增 -->
        <el-button v-permission="['ADMIN','TESTTREE_ALL','TESTTREE_CREATE']" class="filter-item" size="mini" type="primary" icon="el-icon-plus" @click="toAdd">新增</el-button>
        <el-button
          class="filter-item"
          size="mini"
          type="primary"
          icon="el-icon-more"
          @click="expandAll"
        >{{ expand ? '折叠' : '展开' }}</el-button>
      </div>
    </div>
    <div class="head-container">
      <!-- 搜索 -->
      <div class="head-search">
        <el-select v-model="query.type" clearable placeholder="查找类型" class="filter-item" style="width: 130px">
          <el-option v-for="item in queryTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
        <el-input v-model="query.value" clearable placeholder="输入关键字搜索" style="width: 200px;" class="filter-item" @keyup.enter.native="toQuery" />
        <el-date-picker
          v-model="query.createTime"
          :picker-options="pickerOptions"
          class="filter-item"
          size="mini"
          style="height: 31px;line-height:31px;margin-right:10px"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          align="right"
        />
        <el-button class="filter-item" size="mini" type="primary" icon="el-icon-search" @click="toQuery">搜索</el-button>
        <el-button class="filter-item" size="mini" icon="el-icon-refresh" @click="toReSet">重置</el-button>
      </div>
      <!--表格渲染-->
      <tree-table v-loading="loading" :data="data" :expand-all="expand" :columns="columns" size="mini">
        <el-table-column :show-overflow-tooltip="true" align="center" prop="playDate" label="测试时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.playDate) }}</span>
          </template>
        </el-table-column>

        <el-table-column align="center" prop="asName" label="别名" />

        <el-table-column align="center" prop="str1" label="字符串1" />

        <el-table-column align="center" prop="num1" label="数字1" />

        <el-table-column align="center" prop="dou1" label="小数1" />

        <el-table-column v-if="checkPermission(['ADMIN','TESTTREE_ALL}','TESTTREE_EDIT','TESTTREE_DELETE'])" label="操作" align="center" width="200">
          <template slot-scope="scope">
            <el-button v-permission="['ADMIN','TESTTREE_ALL','TESTTREE_EDIT']" size="mini" type="text" @click="toUpdate(scope.row)">修改</el-button>
            <el-popover
              :ref="scope.row.id"
              v-permission="['ADMIN','TESTTREE_ALL','TESTTREE_DELETE']"
              placement="top"
              width="180"
            >
              <p>确定删除吗,如果存在下级节点则一并删除，此操作不能撤销！</p>
              <div style="text-align: right; margin: 0">
                <el-button size="mini" type="text" @click="$refs[scope.row.id].doClose()">取消</el-button>
                <el-button :loading="delLoading" type="primary" size="mini" @click="subDelete(scope.row.id)">
                  确定
                </el-button>
              </div>
              <el-button slot="reference" type="text" style="margin-left:10px" size="mini">删除</el-button>
            </el-popover>
          </template>
        </el-table-column>
      </tree-table>
    </div>
    <eForm
      ref="form"
      :is-add="isAdd"
      :dictobj="dictobj"
    />
  </div>
</template>

<script>
import checkPermission from '@/utils/permission'
import initData from '@/mixins/initData'
import initDict from '@/mixins/initDict'
import { del } from '@/api/generator/testTree/index'
import { parseTime } from '@/utils/index'
import eForm from './module/form'
import treeTable from '@/components/TreeTable'
export default {
  components: {
    eForm,
    treeTable
  },
  mixins: [initData, initDict],
  data() {
    return {
      delLoading: false, supthis: this,
      expand: true,
      columns: [{
        text: '名称',
        value: 'name'
      }],
      queryTypeOptions: [
        { key: 'name', display_name: '名称' },
        { key: 'asName', display_name: '别名' },
        { key: 'str1', display_name: '字符串1' }
      ],
      dictobj: {},
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      }
    }
  },
  created() {
    this.$nextTick(() => {
      this.init()
    })
  },
  methods: {
    parseTime,
    checkPermission,
    beforeInit() {
      this.url = 'api-generator/testTree'
      const sort = 'DESC'
      const sortColumn = 'id'
      this.params = {
        sortColumn: sortColumn,
        sort: sort
      }
      const query = this.query
      const type = query.type
      const value = query.value
      if (query['asName']) {
        this.params['asName'] = query['asName']
      }
      if (query['str1']) {
        this.params['str1'] = query['str1']
      }
      const time = query.createTime
      if (time) {
        if (time.length) {
          this.params['beginTime'] = time[0]
          this.params['endTime'] = time[1]
        }
      }
      if (type && value) {
        this.params['searchKey'] = type
        this.params['searchValue'] = value
      }
      return true
    },
    /**
             * 去新增
             */
    toAdd() {
      this.isAdd = true
      this.$refs.form.resetForm()
      this.$refs.form.getParentData()
      this.$refs.form.dialog = true
    },

    /**
             * 去修改
             */
    toUpdate(data) {
      this.isAdd = false
      const _this = this.$refs.form
      _this.resetForm()
      this.$refs.form.getParentData()
      _this.form = {
        name: data.name,
        pid: data.pid,
        playDate: data.playDate,
        asName: data.asName,
        str1: data.str1,
        num1: data.num1,
        dou1: data.dou1,
        id: data.id
      }
      _this.dialog = true
    },

    /**
             * 去删除
             * @param id
             */
    subDelete(id) {
      this.delLoading = true
      del(id).then(res => {
        this.delLoading = false
        this.$refs[id].doClose()
        this.init()
        this.$notify({
          title: '删除成功',
          type: 'success',
          duration: 2500
        })
      }).catch(err => {
        this.delLoading = false
        this.$refs[id].doClose()
        console.log(err)
      })
    }

  }
}
</script>

<style scoped>
</style>

